//
//  CreatDiaryVc.swift
//  YourName
//
//  Created by 李朋 on 2017/5/19.
//  Copyright © 2017年 李朋. All rights reserved.
//

import UIKit

class CreatDiaryVc: BaseVc {
    
    fileprivate let textField: UITextField = {
        let textField = UITextField(frame:CGRect(x: 0, y: 20, width: 200, height: 30))
        textField.borderStyle = .roundedRect
        textField.placeholder = "请输入文字"
        textField.returnKeyType = .done
        return textField
    }()
    
    fileprivate let saveButton: UIButton = {
        let saveButton = UIButton(frame: CGRect(x: 210, y: 20, width: 60, height: 30))
        saveButton.setTitle("保存", for: .normal)
        saveButton.backgroundColor = UIColor.red
        return saveButton
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }

}

extension CreatDiaryVc {
    
    fileprivate func setupUI() {
        view.addSubview(textField)
        textField.delegate = self
        saveButton.addTarget(self, action: #selector(saveDiary), for: .touchUpInside)
        view.addSubview(saveButton)
    }
    
    @objc fileprivate func saveDiary() {
        textField.resignFirstResponder()
        if textField.text != "" {
            let name = NSNotification.Name(rawValue: "justTest")
            NotificationCenter.default.post(name: name, object: "\(String(describing: textField.text!))");
            FmdbDataManager.shared.updataDiary(diary: String(describing: textField.text!))
        }
        dismiss(animated: true, completion: nil)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
}

extension CreatDiaryVc: UITextFieldDelegate {

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        view.endEditing(true)
        return true
    }
    
}











